package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetVisit;
import com.example.yiwei.crm.entity.scheduleEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.JYAddVisitActivity;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.JYCoordVisitActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.yiwei.crm.R.id.detailed_visit_txt_creater;

public class detailedVisitActivity extends BaseActivity{
    private ImageView img_back,img_update,img_delete;
    private TextView txt_theme,txt_creater,txt_create_time,txt_visit_gift,txt_gift_value,txt_manager,txt_requred,
            txt_visit_time;
    scheduleEntity scheduleEntity;
    SharedPreferences sp;
    int visit_id;
    private ImageView img_lr,img_cx,img_tj,img_me;
    String result;
    private LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_visit);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        txt_requred= (TextView) findViewById(R.id.detailed_visit_txt_director_required);
        img_back= (ImageView) findViewById(R.id.detailed_visit_img_back);
        img_delete= (ImageView) findViewById(R.id.detailed_visit_img_delete);
        img_update= (ImageView) findViewById(R.id.detailed_visit_img_update);
        img_cx= (ImageView) findViewById(R.id.item_image_cx);
        img_lr= (ImageView) findViewById(R.id.item_image_lr);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        txt_theme= (TextView) findViewById(R.id.detailed_visit_txt_theme);
        linearLayout= (LinearLayout) findViewById(R.id.search_linearLayout);
        txt_creater= (TextView) findViewById(detailed_visit_txt_creater);
        txt_create_time= (TextView) findViewById(R.id.detailed_visit_txt_create_time);
        txt_visit_gift= (TextView) findViewById(R.id.detailed_visit_txt_visit_gift);
        txt_gift_value= (TextView) findViewById(R.id.detailed_visit_txt_gift_price);
        txt_manager= (TextView) findViewById(R.id.detailed_visit_txt_manager);
//        txt_state= (TextView) findViewById(R.id.detailed_visit_txt_state);
        txt_visit_time= (TextView) findViewById(R.id.detailed_visit_txt_visit_time);
        scheduleEntity=new scheduleEntity();
        scheduleEntity= GetVisit.scheduleEntity;
        txt_theme.setText(scheduleEntity.getSchedule_theme());
        txt_create_time.setText(scheduleEntity.getCreate_time());
        txt_creater.setText(scheduleEntity.getCurrent_create_user_name());
        txt_visit_time.setText(scheduleEntity.getVisit_time());
        txt_visit_gift.setText(scheduleEntity.getVisit_gift());
        txt_gift_value.setText(scheduleEntity.getGift_value());
        txt_manager.setText(scheduleEntity.getManager_name());
//        txt_state.setText(scheduleEntity.getTask_state());
        visit_id=scheduleEntity.getVisit_id();
        txt_requred.setText(scheduleEntity.getDirector_required());
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if ("JY".equals(getIntent().getStringExtra("JY"))){
            linearLayout.setVisibility(View.VISIBLE);
            img_tj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(detailedVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("tt","tt");
                    startActivity(intent);
                    finish();
                }
            });
            img_me.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(detailedVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("me","me");
                    startActivity(intent);
                    finish();
                }
            });
            img_lr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(detailedVisitActivity.this, JDcoorActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
            img_cx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(detailedVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("ii","ii");
                    startActivity(intent);
                    finish();
                }
            });
        }else{
            linearLayout.setVisibility(View.GONE);
        }
        img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder;
                builder = new AlertDialog.Builder(new ContextThemeWrapper(detailedVisitActivity.this, R.style.custom_dialog));
                builder.setTitle("您确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                if ("coor".equals(getIntent().getStringExtra("TAG"))){
                                    SC sc = new SC();
                                    JSONObject object = sc.storage(sp);
                                    try {
                                        object.put("KPA01",visit_id);
                                        object.put("KXA01",scheduleEntity.getDocking_id());
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    String json = object.toString();
                                    result= NetWorkUtils.getCount(baseClass.delect_coor_bf_URL,json);
                                }else{
                                    result= NetWorkUtils.getCount(baseClass.delete_visit_message_url+visit_id,null);
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject object=new JSONObject(result);
                                            boolean boolen=object.getBoolean("ifsuccess");
                                            if (boolen){
                                                finish();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }).start();

                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();

            }
        });
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(detailedVisitActivity.this,JYAddVisitActivity.class);
                intent.putExtra("tag","update");
                startActivity(intent);
                finish();
            }
        });
    }
}
